package com.nowcoder.community.d_controller;


import com.nowcoder.community.a_entity.Event;
import com.nowcoder.community.a_entity.Page;
import com.nowcoder.community.a_entity.User;
import com.nowcoder.community.c1_kafka.EventProducer;
import com.nowcoder.community.c_service.FollowService;
import com.nowcoder.community.c_service.UserService;
import com.nowcoder.community.z_util.CommunityConstant;
import com.nowcoder.community.z_util.CommunityUtil;
import com.nowcoder.community.z_util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import com.nowcoder.community.z_util.CommunityConstant;

@Controller
public class FollowController implements CommunityConstant{
    @Autowired
    private FollowService followService;

    @Autowired
    private HostHolder hostHolder;

    @Autowired
    private UserService userService;
    @Autowired
    private EventProducer eventProducer;

    @RequestMapping(path = "/follow", method = RequestMethod.POST)
    @ResponseBody
    public String guanzhu(int entityType, int entityId){
        User user = hostHolder.getUser();

        followService.guanzhu(user.getId(),entityType,entityId);//无论关注取关，这里都是3个参数（涉及2个表）

        //kakfa加入message表（复用私信表）+sql
        Event event = new Event()
                .setTopic(TOPIC_FOLLOW)
                .setUserId(hostHolder.getUser().getId())
                .setEntityType(entityType)
                .setEntityId(entityId)
                .setEntityUserId(entityId);
        eventProducer.fireEvent(event);

        return CommunityUtil.javaToJsonString(0,"msg-关注成功！");
    }


    @RequestMapping(path = "/unfollow", method = RequestMethod.POST)
    @ResponseBody
    public String quguan(int entityType, int entityId){
        User user = hostHolder.getUser();

        followService.quguan(user.getId(),entityType,entityId);

        return CommunityUtil.javaToJsonString(0,"msg-取消关注成功了！！");
    }



    /**
     * <p>——————————————————————————————————————————————-————
     * <p> 关注 详细列表
     * <p>——————————————————————————————————————————————-————
     */
    @RequestMapping(path = "/followees/{userId}", method = RequestMethod.GET)
    public String getFollowees(@PathVariable("userId") int userId, Page page, Model model) {
        User user = userService.findUserByid(userId);
        if (user == null) {
            throw new RuntimeException("该用户不存在!");
        }
        model.addAttribute("user", user);

        page.setLimit(5);
        page.setPath("/followees/" + userId);
        page.setRows((int) followService.findFolloweeCount(userId, ENTITY_TYPE_USER));

        List<Map<String, Object>> userList = followService.findFollowees(userId, page.getOffset(), page.getLimit());
        if (userList != null) {
            for (Map<String, Object> map : userList) {
                User u = (User) map.get("user");
                map.put("hasFollowed", followService.hasFollowed(hostHolder.getUser().getId(),
                                                                3,
                                                                userId));//之前ajax实现的前端假的 可关注已关注，这里列表需要真查
            }
        }
        model.addAttribute("users", userList);

        return "moban/site/followee";
    }

    /**
     * <p>——————————————————————————————————————————————-————
     * <p>   粉丝 详细列表
     * <p>——————————————————————————————————————————————-————
     */
    @RequestMapping(path = "/followers/{userId}", method = RequestMethod.GET)
    public String getFollowers(@PathVariable("userId") int userId, Page page, Model model) {
        User user = userService.findUserByid(userId);

        model.addAttribute("user", user);

        page.setLimit(5);
        page.setPath("/followers/" + userId);
        page.setRows((int) followService.findFollowerCount(ENTITY_TYPE_USER, userId));

        List<Map<String, Object>> userList = followService.findFollowers(userId, page.getOffset(), page.getLimit());
        if (userList != null) {
            for (Map<String, Object> map : userList) {
                User u = (User) map.get("user");
                map.put("hasFollowed", followService.hasFollowed(hostHolder.getUser().getId(),
                                                                    3,
                                                                        userId));
            }
        }
        model.addAttribute("users", userList);

        return "moban/site/follower";
    }
}
